import datetime
import data
from datetime import date

from flask import Blueprint, render_template, send_from_directory, request

from analyse import AnalyseData
from services import provinceService

analyseRouter = Blueprint('analyseRouter', __name__)


@analyseRouter.route('/')
def index():
    provinceList = provinceService.list()
    return render_template('analyse/index.html',
                           provinceList=provinceList,
                           length=len(provinceList))


@analyseRouter.route('/chart/<cityId>')
def chart(cityId):
    weeklyList = data.getWeeklyWeather(cityId)
    return render_template('analyse/chart.html',
                           minTempList=[item.minTemp for item in weeklyList],
                           maxTempList=[item.maxTemp for item in weeklyList],
                           dateList=[(item.date if type(item.date) is str else item.date.strftime("%Y-%m-%d")) for item in
                                     weeklyList]
                           )


@analyseRouter.route('/province/<area>/<provinceIdPart>')
def analyse(area, provinceIdPart):
    provinceId = area + "/" + provinceIdPart
    data = AnalyseData.analyseProvince(provinceId)
    today = date.today()
    return render_template('analyse/analyse.html',
                           province=provinceService.getById(provinceId),
                           data=data,
                           length=len(data),
                           datePair=[
                               today.strftime("%Y-%m-%d"), (today + datetime.timedelta(days=6)).strftime("%Y-%m-%d")]
                           )
